package com.youlu.campus.web.config.filter;

import com.youlu.campus.service.system.SystemIpLimitService;
import com.youlu.campus.service.visitor.UserBlackListService;
import com.youlu.campus.service.wechat.WechatConfigInfoService;
import com.youlu.campus.web.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: FilterReConfiguration
 * @Date 2025/7/9
 * @module FilterReConfiguration
 * @author: su
 */
@Slf4j
@Configuration
@ServletComponentScan
public class FilterConfiguration {

    private static final int FIRST_ORDER = 0;//第一个
    private static final int SECOND_ORDER = 1;//第二个
    private static final int THIRD_ORDER = 2;//第三个
    private static final int FOURTH_ORDER = 3;//第四个
    private static final int FIFTH_ORDER = 4;//第五个

    @Value("${server.servlet.context-path}")
    private String contentPath;

    @Autowired
    private SystemIpLimitService systemIpLimitService;

    @Autowired
    private UserBlackListService userBlackListService;

    @Autowired
    private WechatConfigInfoService wechatConfigInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    /**
     * 保活过滤器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean keepAlivedFilter() {
        KeepAlivedFilter keepAlivedFilter = new KeepAlivedFilter(contentPath);
        FilterRegistrationBean bean = new FilterRegistrationBean(keepAlivedFilter);
        bean.setOrder(FIRST_ORDER);
        bean.setUrlPatterns(Arrays.asList("/", "/campus"));
        return bean;
    }

    /**
     * 跨域处理 过滤器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        // 允许的网站域名，全允许则设为 *
        // config.addAllowedOrigin("*");
        config.addAllowedOriginPattern("*");
        config.addAllowedHeader("*");// 允许 HEADER 或 METHOD ， * 为全部
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        // 这个顺序很重要，为避免麻烦请设置在最前
        bean.setOrder(SECOND_ORDER);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }


    /**
     * ip请求限制过滤器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean ipLimitFilter() {
        IpLimitFilter ipLimitFilter = new IpLimitFilter(systemIpLimitService);
        FilterRegistrationBean bean = new FilterRegistrationBean(ipLimitFilter);
        bean.setOrder(SECOND_ORDER);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }

    /**
     * ip请求限制过滤器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean timeFilter() {
        RequestTimeFilter timeFilter = new RequestTimeFilter();
        FilterRegistrationBean bean = new FilterRegistrationBean(timeFilter);
        bean.setOrder(THIRD_ORDER);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }


    @Bean
    public FilterRegistrationBean requestHeaderFilter() {
        RequestHeaderFilter requestHeaderFilter = new RequestHeaderFilter(contentPath, wechatConfigInfoService);
        FilterRegistrationBean bean = new FilterRegistrationBean(requestHeaderFilter);
        bean.setOrder(THIRD_ORDER);
        bean.setUrlPatterns(Arrays.asList("/public/wechat-h5/code", "/public/wechat-h5/code/new", "/mini-app/activity/query"));
        return bean;
    }

    /**
     * app鉴权过滤器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean appAuthFilter() {
        List<String> excludedUrls = new ArrayList<>();
        excludedUrls.add("/");
        excludedUrls.add("/public/**");//排除 public
        excludedUrls.add("/static/**");//排除静态资源
        excludedUrls.add("/kaptcha/**");//排除验证码
        excludedUrls.add("/ca-app/**");//排除证书查询
        excludedUrls.add("/admin/**");//排除 管理后台 认证
        excludedUrls.add("/open/**");//排除 公开接口
        excludedUrls.add("/login");//排除登录

        AuthFilter authFilter = new AuthFilter(Arrays.asList(contentPath), excludedUrls, redisTemplate, userService);
        FilterRegistrationBean bean = new FilterRegistrationBean(authFilter);
        //默认最后在验证用户相关信息
        bean.setOrder(FOURTH_ORDER);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }

    /**
     * 用户 请求限制 过滤器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean userLimitFilter() {
        UserLimitFilter userLimitFilter = new UserLimitFilter(userBlackListService);
        FilterRegistrationBean bean = new FilterRegistrationBean(userLimitFilter);
        bean.setOrder(FIFTH_ORDER);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }

}
